<!DOCTYPE html>
<html class="ui-page-login">

<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title></title>
    <link href="css/mui.min.css" rel="stylesheet"/>
    <link href="css/style.css" rel="stylesheet"/>
    <link rel="stylesheet" type="text/css" href="css/login.css"/>
</head>

<body>
<header class="mui-bar mui-bar-nav">
    <h1 class="mui-title">登录</h1>
</header>
<div class="mui-content">
    <form id='login-form' class="mui-input-group">
        <div class="mui-input-row">
            <label>账号</label>
            <input id='account' type="text" class="mui-input-clear mui-input" placeholder="请输入账号" value="18520606017">
        </div>
        <div class="mui-input-row">
            <label>密码</label>
            <input id='password' type="password" class="mui-input-clear  mui-input" placeholder="请输入密码" value="11111">
        </div>
    </form>
    <form class="mui-input-group">
        <ul class="mui-table-view mui-table-view-chevron">
            <li class="mui-table-view-cell">
                自动登录
                <div id="autoLogin" class="mui-switch">
                    <div class="mui-switch-handle"></div>
                </div>
            </li>
        </ul>
    </form>

    <div class="mui-content-padded">
        <button id='login' type="button" data-loading-text="登录中..." class="mui-btn mui-btn-block mui-btn-primary">登录
        </button>
        <div class="link-area" style="display: none;">
            <a id='reg'>注册账号</a> <span class="spliter">|</span>
            <a id='forgetPassword'>忘记密码</a>
        </div>
    </div>
    <div class="mui-content-padded oauth-area" style="display: none;">

    </div>

    <form class="mui-input-group">
        <button id='clear' data-loading-text="清除缓存中..." class="mui-btn mui-btn-block mui-btn-primary">清除缓存</button>
    </form>

</div>
<script src="js/mui.min.js"></script>
<script src="js/mui.enterfocus.js"></script>
<script src="js/jquery-3.2.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/common.js"></script>
<script src="js/jpush.js"></script>
<!--<script src="js/app.js"></script>-->
<script>
    (function ($, doc) {
        $.init({
            statusBarBackground: '#f7f7f7'
        });
        mui.plusReady(function () {
            //关闭等待框
            plus.nativeUI.closeWaiting();
            //显示当前页面
            mui.currentWebview.show();


            var onOpenNotification = function (event) {
                var alertContent;
                if (plus.os.name == "Android") {
                    alertContent = window.plus.Push.openNotification.alert;
                } else {
                    alertContent = event.aps.alert;
                }
                mui.alert("Open notification:" + alertContent);
            }

            var onReceiveNotification = function (event) {
                var alertContent;
                if (plus.os.name == "Android") {
                    alertContent = window.plus.Push.receiveNotification.alert;
                } else {
                    alertContent = event.aps.alert;
                }
                mui.alert("Open notification:" + alertContent);
            }

            document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);

            document.addEventListener("jpush.openNotification", onOpenNotification, false);
        })
        mui.ready(function () {
            jQuery("body").on('click', '.mui-btn', function (e) {
                var msg = jQuery(this).attr('data-loading-text');
                var msg1 = jQuery(this).html();
                jQuery(this).attr("disabled", "disabled");
                jQuery(this).addClass("mui-disabled");
                jQuery(this).html(msg + "&nbsp;&nbsp;&nbsp;<span class='mui-spinner'></span>");
                setTimeout(function () {
                    jQuery(this).html(msg1);
                    jQuery(this).removeClass("mui-disabled");
                    jQuery(this).attr("disabled", false);
                }.bind(this), 3000);
            });
        })
        $.plusReady(function () {

            plus.screen.lockOrientation("portrait-primary");
            //					自动登录

            document.getElementById("clear").addEventListener("tap", function (event) {
                localStorage.clear();
                //登出
                //						mui.ajax(url + "/app/user/logout", {
                //							data: {
                //								username: accountBox.value,
                //								password: passwordBox.value
                //							},
                //							dataType: 'json', //服务器返回json格式数据
                //							type: 'post', //HTTP请求类型
                //							timeout: 10000, //超时时间设置为10秒；
                //							headers: {
                //								'Content-Type': 'application/json'
                //							},
                //							success: function(data) {
                //								//服务器返回响应，根据响应结果，分析是否登录成功；
                //								console.log(JSON.stringify(data));
                //								if(data.success) {
                //									localStorage.clear();
                //								} else {
                //									alert(data.msg);
                //								}
                //
                //							},
                //							error: function(xhr, type, errorThrown) {
                //								//异常处理；
                //								console.log(JSON.stringify(xhr));
                //								console.log(type);
                //								console.log(errorThrown);
                //							}
                //						});

            })

            document.getElementById("autoLogin").addEventListener("toggle", function (event) {
                if (event.detail.isActive) {
                    mui.toast('您开启了自动登录', {
                        duration: 'long',
                        type: 'div'
                    })
                    localStorage.setItem("autoLogin", "true")
                } else {
                    mui.toast('您关闭了自动登录', {
                        duration: 'long',
                        type: 'div'
                    })

                    localStorage.setItem("autoLogin", "false")
                }
            })

            if (localStorage.getItem("autoLogin") == "true") {
                document.getElementById("autoLogin").classList.add("mui-active");

                var index = plus.webview.getLaunchWebview();
                plus.webview.reload(index);

            } else if (localStorage.getItem("autoLogin") == "false") {
                document.getElementById("autoLogin").classList.remove("mui-active");

            }
            //登陆
            var loginButton = doc.getElementById('login');
            var accountBox = doc.getElementById('account');
            var passwordBox = doc.getElementById('password');
            loginButton.addEventListener('tap', function (event) {
                //			ajax
                console.log(accountBox.value);
                console.log(url + "/app/user/login");
                mui.ajax(url + "/app/user/login", {
                    data: {
                        username: accountBox.value,
                        password: passwordBox.value
                    },
                    dataType: 'json', //服务器返回json格式数据
                    type: 'post', //HTTP请求类型
                    timeout: 10000, //超时时间设置为10秒；
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    success: function (data) {
                        //服务器返回响应，根据响应结果，分析是否登录成功；
                        console.log(JSON.stringify(data));

                        if (data.code == 0) {

                            localStorage.setItem("token", data.data);
                            mui.ajax(url + "/app/user/info?token=" + data.data, {
                                type: 'post', //HTTP请求类型
                                async: false,
                                data: {},
                                dataType: 'json', //服务器返回json格式数据
                                timeout: 10000, //超时时间设置为10秒；
                                headers: {
                                    'Content-Type': 'application/json'
                                },
                                success: function (data) {
                                    //服务器返回响应，根据响应结果，分析是否登录成功；
                                    console.log(JSON.stringify(data));
                                    if (data.code == 0) {
                                        setTagsWithAlias();
                                        function setTagsWithAlias() {
                                            plus.Push.setTagsWithAlias([], data.data.userId)
                                        };
                                        document.addEventListener("jpush.setTagsWithAlias", onAliasAndTagsSet, false);
                                        function onAliasAndTagsSet(event) {
                                            var result = "result code: " + event.arguments.resultCode + " "
                                            result += "tags: " + event.arguments.tags + " "
                                            result += "alias: " + event.arguments.alias + " "
                                            alert(result)
                                            mui.toast("别名设置成功，" + result);
                                        };
                                        //									清除view
                                        var vw_list = plus.webview.all();
                                        for (var i = 0, len = vw_list.length; i < len; i++) {
                                            var ws = plus.webview.getWebviewById(vw_list[i].id);
                                            var length = vw_list[i].id.length - 10;
                                            // alert(vw_list[i].id);
                                            if (vw_list[i].id == "HBuilder" || vw_list[i].id == "H520920DF") {
                                                setTimeout(function () {
                                                    var index = plus.webview.getLaunchWebview();
                                                    index.reload();
                                                }, 50)
                                            } else if (vw_list[i].id.slice(length) == "login.html") {
                                                plus.webview.hide(ws)
                                                setTimeout(function () {
                                                    plus.webview.close(ws);
                                                }, 50)
                                            } else {
                                                plus.webview.close(ws);
                                            }
                                        }
                                    } else if (data.code == 500 && data.msg == "令牌已经失效,请重新登录") {
                                        plus.nativeUI.toast('网络连接错误，请重新登录');
                                        mui.openWindow({
                                            url: 'login.html',
                                            show: {
                                                autoShow: true
                                            }
                                        });
                                    } else {
                                        alert(data.msg)
                                    }
                                },
                                error: function (xhr, type, errorThrown) {
                                    //异常处理；
                                    console.log(type);
                                    plus.nativeUI.toast('网络连接错误，请重新登录');
                                    mui.openWindow({
                                        url: 'login.html',
                                        show: {
                                            autoShow: true
                                        }
                                    });
                                }
                            });

                        } else if (data.code != 0) {
                            alert("token获取失败")
                        } else {
                            alert(data.msg);
                        }

                    },
                    error: function (xhr, type, errorThrown) { //异常处理；
                        console.log(JSON.stringify(xhr));
                        console.log(type);
                        console.log(errorThrown);
                        mui.toast(xhr.status)
                    }
                });

            });
            //					安卓返回键返回事件
            var backButtonPress = 0;
            $.back = function (event) {
                backButtonPress++;
                if (backButtonPress > 1) {
                    plus.runtime.quit();
                } else {
                    plus.nativeUI.toast('再按一次退出应用');
                }
                setTimeout(function () {
                    backButtonPress = 0;
                }, 1000);
                return false;
            };
        });
    }(mui, document));
</script>
</body>

</html>